import pyglet
from pyglet.gl import *
from pyglet.image import AnimationFrame


circ32 = pyglet.graphics.vertex_list(32, ('v2f', (1,0,0.980785, 0.19509, 0.92388, 0.382683, 0.83147, 0.55557, 0.707107, 0.707107, 0.55557, 0.83147, 0.382683, 0.92388, 0.19509, 0.980785, 0, 1, -0.19509, 0.980785, -0.382683, 0.92388, -0.55557, 0.83147, -0.707107, 0.707107, -0.83147, 0.55557, -0.92388, 0.382683, -0.980785, 0.19509, -1, 0, -0.980785, -0.19509, -0.92388, -0.382683, -0.83147, -0.55557, -0.707107, -0.707107, -0.55557, -0.83147, -0.382683, -0.92388, -0.19509, -0.980785, 0, -1, 0.19509, -0.980785, 0.382683, -0.92388, 0.55557, -0.83147, 0.707107, -0.707107, 0.83147, -0.55557, 0.92388, -0.382683, 0.980785, -0.19509)))

def draw_circle(x0, y0, r):
    glPushMatrix()
    glTranslatef(x0,y0,0)
    glScalef(r,r,0)
    if r < 40: circ16.draw(GL_LINE_LOOP)
    else: circ32.draw(GL_LINE_LOOP)
    glPopMatrix()

def drawPolyShape(poly):
    body = poly.body
    
    glBegin(GL_LINE_LOOP)
    for vert in poly.verts:
        v = body.position + poly.offset + vert.cpvrotate(body.rotation_vector)
        glVertex2f(v.x, v.y)
    glEnd()

def animation_frame(filename, anchor_x, anchor_y, duration):
    image = pyglet.resource.image(filename)
    image.anchor_x = anchor_x
    image.anchor_y = anchor_y
    return AnimationFrame(image, duration)

def make_sprite(filename, anchor_x=0, anchor_y=0):
    image = pyglet.resource.image(filename)
    image.anchor_x = anchor_x
    image.anchor_y = anchor_y
    sprite = pyglet.sprite.Sprite(image)
    # Fix texture parameters for sprite before returning it
    glEnable(sprite._texture.target)
    glBindTexture(sprite._texture.target, sprite._texture.id)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
    return sprite
